package make.money;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import make.money.Files.FileUtils;
import make.money.gold.DoubleBoolWhere;
import make.money.gold.ListUtils;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class bool {
    public static String filePath = "D:\\test.txt";
    private static String filePathMoney = "D:\\money.txt";

    private static String winPath = "D:\\doublemoney.txt";
    public static void main(String[] args) throws Exception{
//        List<String> a = Arrays.asList("3,8,12,21,26,32",
//                "3,8,16,19,26,30","3,8,16,25,28,32","3,10,12,20,26,31");
//        List<String> b = Arrays.asList("3,16,18,26,27,32");
//        System.out.println(getMoneyYun(a,b,4));
        int sum = 6316;
        sum = zhongjianshu();
//        Thread.sleep(1000);
//        int count =0;
//        while(true) {// 3505
//            getMoney(1,6074,100, 4);
//            List<String> money = readFile(filePathMoney);
//            count++;
//            if(money.size()>=200 || count>=100){
//                break;
//            }
//        }

//        sortFile(winPath);
    }

    public static void sortFile(String path) throws Exception{
        List<String> money = readFile(path);

        Thread.sleep(1000);
        money = money.stream().sorted().distinct().collect(Collectors.toList());

//        money = money.stream().sorted().distinct().collect(Collectors.toList());
        FileUtils.createFile(path);
        Thread.sleep(1000);
        saveMoney(money,false,path);
    }

    /**
     * 按条件生成可能得条件 并存到pathString中
     * @return
     * @throws Exception
     */
    private static int zhongjianshu() throws Exception{
        //判断文件1是否存在不存在就创建他
        FileUtils.createFile(filePathMoney);
        FileUtils.createFile(filePath);

        File file =new File(filePath);
        BufferedWriter bu=new BufferedWriter(new FileWriter(file,true));
        int sum = 0;
        for(int ge = 1;ge<=33;ge++ ){
            for(int shi=ge+1;shi<=33;shi++){
                for(int bai=shi+1;bai<=33;bai++){
                    for(int qian=bai+1;qian<=33;qian++){
                        for(int wan=qian+1;wan<=33;wan++){
                            for(int yi=wan+1;yi<=33;yi++){
                                if(
                                        whereMoney(Arrays.asList(ge,shi,bai,qian,wan,yi))
                                ){
                                    sum++;
                                    bu.write(ge+","+shi+","+bai+","+qian+","+wan+","+yi);
                                    bu.newLine();
                                }
                            }
                        }
                    }
                }
            }
        }
        bu.close();
        System.out.println("总数："+sum);
        return sum;
    }


    public static boolean whereMoney(List<Integer> moneyList){
        moneyList = moneyList.stream().sorted().collect(Collectors.toList());
        Boolean moneyFlag=

                //奇偶数
                        DoubleBoolWhere.jiou(moneyList,Arrays.asList(2))
//                        &&(moneyList.get(0)==2 ||moneyList.get(0)==4)
                        //排除不要的数字
//                        &&DoubleBoolWhere.notNum(moneyList,Arrays.asList(
//                                2,4,6,9,10,11,14,15,18,19,20,23,27,33
//                        ))
                        &&moneyList.get(5)>27
                        && DoubleBoolWhere.getFanwei(moneyList,
                                Arrays.asList(1,3,5,7,9,11,15,17,19,21,23,25,27,29,31,33,
                                6,8,14,16,22,24,28,26,32),6,6)

//                        &&DoubleBoolWhere.notNum(moneyList,Arrays.asList(1,19,33))
                        // 和的取值范围
                        && DoubleBoolWhere.sumNum(moneyList,90,130)
                        // 和值的尾号 (1,2 代表要取余10 == 1 || 2)1,3 6,8
                        && DoubleBoolWhere.sumWeiNum(moneyList,Arrays.asList(3,4,5,6,7,8,
                                9))


                        && DoubleBoolWhere.getFanwei(moneyList,
                                Arrays.asList(3,8,10,20,30,31),0,1)
                        && DoubleBoolWhere.getFanwei(moneyList,
                                Arrays.asList(4,10,12,13,18,30),0,1)
                        // 求两个集合并集的个数是否符合>=small&&<=big （近1,2期）
                        && DoubleBoolWhere.getFanwei(moneyList,
                        Arrays.asList(4,10,12,13,18,30,3,8,10,20,30,31),1,2)
                        // 求两个集合并集的个数是否符合>=small&&<=big （近3,4,5期）
                        && DoubleBoolWhere.getFanwei(moneyList,
                        Arrays.asList(1,2,5,6,7,9,11,14,15,16,17,21,22,23,25,27),1,3)
                        // 求两个集合并集的个数是否符合>=small&&<=big （其他期）
                        && DoubleBoolWhere.getFanwei(moneyList,
                        Arrays.asList(19,24,26,28,29,32,33),1,2)


                        // 有可能变的因素 放下面

                        //去掉集合中尾号一样的数（超过3个的数）
                        &&  DoubleBoolWhere.weihao(moneyList)
                        // 去掉尾号双重复如（1,11 ，2,22）
                        &&  DoubleBoolWhere.weihaoDouble(moneyList)

                        // 一定不变的放下面


                        // 去掉两个大于30
                        && DoubleBoolWhere.getCount(moneyList,30)
                        // 连续号不要
                        && DoubleBoolWhere.lianxu(moneyList)
                        // 范围10-19不能超过4个
                        && DoubleBoolWhere.getFanweiGeshu(moneyList,10,19,4)
                        && DoubleBoolWhere.getFanweiGeshu(moneyList,1,9,4)
                        && DoubleBoolWhere.getFanweiGeshu(moneyList,20,29,4)
                        && moneyList.get(0)<=9
                        && moneyList.get(2)>9
                        && moneyList.get(2)<20
                        && moneyList.get(5)>=26
                ;
        return moneyFlag;
    }

    /**
     * 获取结果
     */
    private static void getMoney(int min ,int max , int count ,int togetCount) throws Exception{
        //获取随机数
        List<Integer> suijiResult = suijishu(min,max,count);

        //读取文件
        List<String> fileYun = readFile(filePath);
        List<String> moneyYun = readFile(filePathMoney);

        // 获取随机结果
        List<String> suijiYun = ListUtils.getSuijiYun(suijiResult,fileYun);
//        suijiYun = getsuijiList(suijiYun,togetCount); // 自身便利排除4个一样的
        // 遍历集合2 判断有没有相同数 有就不存
        List<String> makeMoney = getMoneyYun(moneyYun,suijiYun,togetCount);
        // 存入文件中
        if(CollUtil.isNotEmpty(moneyYun) ){
            saveMoney(makeMoney,true ,filePathMoney );
        }else{
            saveMoney(makeMoney,false ,filePathMoney);
        }



    }

    /**
     * 存入文件中
     * @param makeMoney
     * @throws Exception
     */
    private static void saveMoney(List<String> makeMoney ,Boolean flag,String path) throws Exception{

        File file =new File(path);

        BufferedWriter bu=new BufferedWriter(new FileWriter(file,true));
        for(String line : makeMoney) {
            if(flag){bu.newLine();}
            bu.write(line);
            flag = true;
        }
        bu.close();
    }
    /**
     * 遍历集合2   判断集合中和集合2中相同的个数超过count就不得到
     * @param moneyYun
     * @param suijiYun
     * @param count
     * @return
     */
    public static List<String> getMoneyYun(List<String> moneyYun,List<String> suijiYun,int count){
        return suijiYun.stream().filter(data->{
            if(CollUtil.isNotEmpty(moneyYun)) {
                for (String money : moneyYun) {
                    if(StrUtil.isBlank(money)){continue;}
                    List<Integer> datas = stringToList(data);
                    List<Integer> moneys = stringToList(money);
                    List<Integer> jieguo = ListUtils.intersect(datas, moneys);
                    if (jieguo.size() >= count) {
                        return false;
                    }
                }
            }
            return true;
        }).collect(Collectors.toList());
    }

    private static List<String> getsuijiList(List<String> suijiYun,int count){
        List<String> stringList = suijiYun;
        List<String> result = new ArrayList<>();
        suijiYun.stream().forEach(data->{
            int sun = 0 ;
            for (String s : stringList){
                List<Integer> datas = stringToList(data);
                List<Integer> moneys = stringToList(s);
                List<Integer> jieguo = ListUtils.intersect(datas, moneys);
                if (jieguo.size() >= count) {
                    sun++;
                }
            }
            if(sun<2){result.add(data);}
        });
        return result;
    }
    /**
     * string 装成List<Integer>
     * @param s
     * @return
     */
    private static List<Integer> stringToList(String s){
        List<String> array =Arrays.asList( s.split(","));
        List<Integer> result = new ArrayList<>();
        array.stream().forEach(data->{
            result.add(Integer.parseInt(data));
        });
        return result;
    }

    // 生成随机数
    private static List<Integer> suijishu(int min,int max,int suijiCount) throws Exception{
        Set<Integer> uniqueNumbers = generateUniqueRandomNumbers(min,max , suijiCount);
        List<Integer> list = uniqueNumbers.stream().sorted().collect(Collectors.toList());
        System.out.println(list);
        return list;
    }

    /**
     * 随机生成数字
     * @param min
     * @param max
     * @param count
     * @return
     */
    public static Set<Integer> generateUniqueRandomNumbers(int min, int max, int count) {
        Random random = new Random();
        Set<Integer> uniqueNumbers = new HashSet<>();
        while (uniqueNumbers.size() < count) {
            int randomNumber = random.nextInt(max - min + 1) + min;
            uniqueNumbers.add(randomNumber);
        }
        return uniqueNumbers;
    }

    /**
     * 将集合存到新的路径中
     * @throws Exception
     */
    public static List<String> readFile(String filePathYun) throws Exception{
        List<String> newString = new ArrayList<>();
//        Thread.sleep(3000);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(filePathYun));
            String line;
            while ((line = reader.readLine()) != null) {
                // 处理读取到的每一行数据
                newString.add(line);
            }

        } catch (IOException e) {
            System.out.println("Error:"+e.getMessage());
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return newString;
    }


//    public static void main(String[] args) {
////        Set<Integer> uniqueNumbers = generateUniqueRandomNumbers(1, 10000, 50);
////        List<Integer> list = uniqueNumbers.stream().sorted().collect(Collectors.toList());
////        System.out.println(list);
////        readFile(list);
//
//    }


}
